package com.ewili.x.special.service.data.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.framework.utils.ExceptionUtils;
import com.ewili.framework.utils.StringUtils;
import com.ewili.x.special.dao.inter.FinanceNoticeDao;
import com.ewili.x.special.entity.FinanceNotice;
import com.ewili.x.special.service.data.inter.FinanceNoticeService;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;

@Service
public class FinanceNoticeServiceImpl implements FinanceNoticeService {

    @Autowired
    private FinanceNoticeDao financeNoticeDao;


    @Override
    public void spideFinanceNotice() {
        for (int i = 1; i < 300; i++) {
            String url = "http://datacenter.eastmoney.com/api/data/get?callback=jQuery112306727236097795996_1606127427313&st=NOTICE_DATE%2CSECURITY_CODE&sr=-1%2C-1&ps=50&p=" + i + "&type=RPT_PUBLIC_OP_PREDICT&sty=ALL&token=894050c76af8597a853f5b408b759f5d";
            boolean shouldBreak = this.doSpide(url);
            if (shouldBreak) {
                break;
            }
        }
    }

    private boolean doSpide(String url) {
        Connection connection = Jsoup.connect(url).timeout(30000000);
        connection.maxBodySize(0);

        String content = null;
        try {
            content = connection.get().body().text();
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        }


        if (!content.contains("SECURITY_CODE")) {
            return true;
        }

        content = StringUtils.substringBetween(content, "(", "});") + "}";
        JSONObject jsonObject = JSON.parseObject(content);
        jsonObject = jsonObject.getJSONObject("result");
        JSONArray arr = jsonObject.getJSONArray("data");

        for (int j = 0; j < arr.size(); j++) {
            JSONObject obj = arr.getJSONObject(j);
            String code = obj.getString("SECURITY_CODE");
            Date noticeDate = obj.getSqlDate("NOTICE_DATE");
            Date reportDate = obj.getSqlDate("REPORTDATE");
            Double downForeCastt = obj.getDouble("FORECASTL");
            Double upForeCastt = obj.getDouble("FORECASTT");
            Double downEarnChangeRate = obj.getDouble("INCREASEL");
            Double upEarnChangeRate = obj.getDouble("INCREASET");
            String foreCasttContent = obj.getString("FORECASTCONTENT");
            String changeReasonScrpt = obj.getString("CHANGEREASONDSCRPT");
            String foreCasttType = obj.getString("FORECASTTYPE");
            Double yearEarLier = obj.getDouble("YEAREARLIER");
            String publishName = obj.getString("PUBLISHNAME");

            FinanceNotice financeNotice = financeNoticeDao.findByCodeAndReportDate(code, reportDate);

            if (financeNotice == null) {
                financeNotice = new FinanceNotice();
            }
            financeNotice.setCode(code);
            financeNotice.setNoticeDate(noticeDate);
            financeNotice.setReportDate(reportDate);
            financeNotice.setDownForeCastt(downForeCastt);
            financeNotice.setUpForeCastt(upForeCastt);
            financeNotice.setDownEarnChangeRate(downEarnChangeRate);
            financeNotice.setUpEarnChangeRate(upEarnChangeRate);
            financeNotice.setForeCasttContent(foreCasttContent);
            financeNotice.setChangeReasonScrpt(changeReasonScrpt);
            financeNotice.setForeCasttType(foreCasttType);
            financeNotice.setYearEarLier(yearEarLier);
            financeNotice.setPublishName(publishName);

            financeNoticeDao.save(financeNotice);
        }
        return false;


    }
}
